package simple;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 剑指offer
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
 * 分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 * 两个栈实现队列
 * 队列：先进先出
 * 栈：先进后出
 */
public class TwoStackMadeQueue {
    private Deque<Integer> inStack;
    private Deque<Integer> outStack;
    public TwoStackMadeQueue() {
        this.inStack = new ArrayDeque<>();
        this.outStack = new ArrayDeque<>();
    }

    public void appendTail(int value) {
        inStack.add(value);
    }

    public int deleteHead() {
        while(!inStack.isEmpty()){
            outStack.add(inStack.pop());
        }
        if (outStack.isEmpty()){
            return -1;
        }
        return outStack.pop();
    }
    public static void main(String[] args) {
        TwoStackMadeQueue myQueue = new TwoStackMadeQueue();
        myQueue.appendTail(1);
        myQueue.appendTail(2);
        System.out.println(myQueue.deleteHead());
        System.out.println(myQueue.deleteHead());
        System.out.println(myQueue.deleteHead());
    }
}
